let server = require("express")();
let expressStatic = require("express-static")
let mysql = require("mysql")
const port = 8080;

let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'sms',
    timezone: "08:00"
});

sql.connect();

server.get("/huoqu", (request, response) => {
    sql.query(`SELECT * FROM student`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send(JSON.stringify(data))
        }
    })
})

server.get("/shanchu", (request, response) => {
    sql.query(`DELETE FROM student WHERE id=${request.query.id}`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
        }
    })
})




server.get("/xinzeng", (request, response) => {
    let p = request.query;
    sql.query(`INSERT INTO student (name,age,sex,city,joinDate) VALUES ("${p.name}","${p.age}","${p.sex}","${p.city}","${p.joinDate}")`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
            console.log(`新增学生${p.name}操作成功`)
        }
    })
})


server.use(expressStatic(__dirname + '/static'))

server.listen(port);
console.log(`server is running at ${port}`)